<template>
  <div>
    <van-nav-bar
      title="详情"
      left-text="返回"
      left-arrow
      @click-right="gengd"
      @click-left="onClickLeft"
    >
      <template #right>
        <van-popover
          v-model="showPopover"
          theme="dark"
          :actions="actions"
          @select="onSelect"
          placement="bottom-end"
        >
          <template #reference>
            <van-icon name="weapp-nav" size="18" />
          </template>
        </van-popover>
      </template>
    </van-nav-bar>
    <!-- 分享 -->
    <van-share-sheet
      v-model:show="showShare"
      title="立即分享给好友"
      :options="options"
      @select="onSelect"
    />
    <div>
      <div @click="showimg">
        <van-swipe :autoplay="2000" lazy-render>
          <van-swipe-item v-for="image in images" :key="image">
            <img :src="image" />
          </van-swipe-item>
        </van-swipe>
      </div>
      <p class="p1">￥{{ list.originprice }}</p>
      <p>{{ list.proname }}</p>
    </div>
    <!-- 气泡弹出框 -->
    <van-action-bar>
      <van-action-bar-icon icon="cart-o" text="购物车" :badge="goddslength" />
      <van-action-bar-button type="warning" text="加入购物车" @click="addcar" />
      <van-action-bar-button type="danger" text="立即购买" />
    </van-action-bar>
  </div>
</template>

<script setup lang="ts">
import { showImagePreview, type ToastOptions } from "vant";
import { showToast } from "vant";
import { getdetail, setaddcar, getgoodslist } from "../../service/user";
import { ref, onMounted, type Ref } from "vue";
// import { useStore } from "vuex";
import { useRoute, useRouter } from "vue-router";
import { useCounterStore } from "@/stores/counter";
// const store = useStore();
let store =useCounterStore();
const router = useRouter();
const route = useRoute();
const goddslength = ref("");
const images = ref<any>([]);
const list = ref<any>([]);
const showPopover = ref<any>(false);
const showShare = ref<any>(false);
const actions =<any> [
  { text: "首页", icon: "wap-home-o" },
  { text: "购物车", icon: "cart-o" },
  { text: "分享", icon: "share-o" },
];
const options = [
  [
    { name: "微信", icon: "wechat" },
    { name: "朋友圈", icon: "wechat-moments" },
    { name: "微博", icon: "weibo" },
    { name: "QQ", icon: "qq" },
  ],
  [
    { name: "复制链接", icon: "link" },
    { name: "分享海报", icon: "poster" },
    { name: "二维码", icon: "qrcode" },
    { name: "小程序码", icon: "weapp-qrcode" },
  ],
];
// const showImagePreview = ref([]);
onMounted(() => {
  getdel();
  store.shownav=false;
  getdgoodslist();
});
//轮播图
function getdel() {
  getdetail({proid:route.params.proid}).then((res) => {
    if ((res.data.code = "200")) {
      images.value = [
        res.data.data.img1,
        res.data.data.img2,
        res.data.data.img3,
        res.data.data.img4,
      ];
      list.value = res.data.data;
    }
    console.log(res);
  });
}

function onClickLeft() {
  history.back();
}
function gengd() {
  showPopover.value = !showPopover.value;
}
//路由跳转
function onSelect(action: { text: string | ToastOptions | undefined; }) {
  showToast(action.text);
  if (action.text === "分享") {
    showShare.value = true;
  } else if (action.text === "首页") {
    router.push("/home");
  } else {
    router.push("/goodsn");
  }
}
//添加购物车
//如果没有登陆需要先跳转登录页
function addcar() {
  if (localStorage.getItem("token")) {
    setaddcar({
      userid: store.userid,
      proid: route.params.proid,
      num: 1,
    }).then((res) => {
      console.log(res)
      if (res.data.code === "200") {
        showToast({
          message: "成功添加购物车",
          icon: "success",
        });
        getdgoodslist();
      }
    });
  } else {
    showToast({
      message: "请先登录",
      icon: "cross",
    });
    router.push("/login");
  }
}
//图片预览
function showimg() {
  showImagePreview({
    images: images.value,
    closeable: true,
    showIndex: true,
  });
}
// 获取购物车数据
// 显示购物车数量
function getdgoodslist() {
  if (localStorage.getItem("token")) {
    getgoodslist({ userid: store.userid }).then((res) => {
      if (res.data.code === "200") {
        goddslength.value = res.data.data.length;
      }
    });
  } else {
    goddslength.value = "";
  }
}
</script>

<style lang="scss" scoped>
img {
  width: 100%;
}
p {
  font-size: 18px;
  font-weight: 900;
}
.p1 {
  font-size: 16px;
  margin: 10px 20px;
  font-weight: 700;
  color: red;
}
</style>